package net.zjitc.DownLoad;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;


/**
 * @Author admin
 * @Date 2019/4/10 10:27
 */
@Controller
public class DownLoadController {
    @RequestMapping("/download")
    public void download(String fileName,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //通过request拿到项目的路径
        String path = request.getServletContext().getRealPath("/WEB-INF/");
        //创建文件对象
        File file = new File(path,fileName);

        System.out.println(file);
        //告诉下载 header

        String str = new String(fileName.getBytes(), "iso-8859-1");
        response.addHeader("content-disposition", "attachment;filename=" + str);
        //将文件输入出前端 IO
        //读取文件内容inputsteam , 输出outputstream
        //new FileInputStream(file)
        //response.getOutputStream()
        IOUtils.copy(new FileInputStream(file),response.getOutputStream());
    }
}
